package com.controller;

import com.pojo.Message;
import com.pojo.PageBean;
import com.pojo.TabAddress;
import com.pojo.User;
import com.service.TabAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/address")
@ResponseBody
public class TabAddressController {
    @Autowired
    private TabAddressService tabAddressService;
    @Autowired
    private HttpSession session;
    @RequestMapping("/getMyAddress")
    public PageBean findMyAddress(Integer current,Integer size,String contact,Integer uid,String address){
        contact=contact!=null?contact:"";
        address=address!=null?address:"";
        User user = (User) session.getAttribute("user");
        System.out.println("重新获取");
        System.out.println(user);
        return tabAddressService.findMyAddress(current,size,contact,uid,address);
    }
    @RequestMapping("/addAddress")
    public Message addAddress(@RequestBody TabAddress tabAddress){
        Message message=new Message();
        int row = tabAddressService.addAddress(tabAddress);
        if(row>0){
            message.setCode("200");
            message.setMsg("添加成功！");
        }else {
            message.setCode("error");
            message.setMsg("添加失败！");
        }
        return message;
    }

    @RequestMapping("/updateAddress")
    public Message updateAddress(@RequestBody TabAddress tabAddress){
        System.out.println(tabAddress);
        Message message=new Message();
        int row = tabAddressService.updateAddress(tabAddress);
        if(row>0){
            message.setCode("200");
            message.setMsg("修改成功！");
        }else{
            message.setCode("error");
            message.setMsg("修改失败！");
        }
        return message;
    }
    @RequestMapping("/updateDefultAddress")
    public Message updateDefultAddress(Integer aid,Integer uid){
        Message message = new Message();
        int row = tabAddressService.updateDefultAddress(aid, uid);
        if(row>0){
            message.setCode("200");
            message.setMsg("设置成功！");
        }else{
            message.setCode("error");
            message.setMsg("设置失败！");
        }
        return message;
    }

    @RequestMapping("/deleteAddress")
    public Message deleteAddress(Integer aid){
        int row = tabAddressService.deleteAddress(aid);
        Message message=new Message();
        if(row>0){
            message.setCode("200");
            message.setMsg("删除成功！");
        }else {
            message.setCode("error");
            message.setMsg("删除失败！");
        }
        return message;
    }
}
